package com.J1M2.LIVE;

import static android.app.PendingIntent.getActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;


import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;


public class opeinstall extends ReactContextBaseJavaModule {

    public static String TAG = "J119989";
    public static String RAF = "";

    public static String PushToken = "";

    private String CODE;

    public opeinstall(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @ReactMethod
    public void getAppflyerAppKey(Callback callback) {
        Context context = getReactApplicationContext();
        String appKey = null;

        try {
            PackageManager packageManager = context.getPackageManager();
            String packageName = context.getPackageName();
            ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            Bundle metaData = appInfo.metaData;

            if (metaData != null) {

                // 获取指定名称的 meta-data 值
                appKey = metaData.getString("AppflyerAPPKEY");
//                Log.d("2222MetaDataHelper", "222AppflyerAPPKEY: " + appKey);
            }
//            Log.d("11111MetaDataHelper", "AppflyerAPPKEY: " + appInfo.metaData);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        // 将获取到的 appKey 通过 Callback 返回给 React Native 端
        callback.invoke(appKey);
    }

    @ReactMethod
    public void getAffCode(Callback callback) {
        Context context = getReactApplicationContext();
        String appKey = null;

        try {
            PackageManager packageManager = context.getPackageManager();
            String packageName = context.getPackageName();
            ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            Bundle metaData = appInfo.metaData;

            if (metaData != null) {

                // 获取指定名称的 meta-data 值
                TAG = metaData.getString("AppAffcode");
//                Log.d("2222MetaDataHelper", "222AppflyerAPPKEY: " + appKey);
            }
//            Log.d("11111MetaDataHelper", "AppflyerAPPKEY: " + appInfo.metaData);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if(TAG == null || TAG.equals("")) {
            //TInstall代理码affcode
            TAG = com.J1M2.LIVE.MainActivity.Affcodes;
        }
        // 将获取到的 appKey 通过 Callback 返回给 React Native 端
       callback.invoke(TAG);
    }

    @Override
    public String getName() {
        return "Openinstall";
    }

    @ReactMethod
    public void getRafCode(Callback callback) {
        //推荐好友code
        RAF = com.J1M2.LIVE.MainActivity.Rafcodes;
        callback.invoke(RAF);
    }

    @ReactMethod
    public void getDeviceToken(Callback callback) {
        //推荐好友code
        PushToken = com.J1M2.LIVE.UMPush.PushHelper.DeviceTokens;
        callback.invoke(PushToken);
    }

    @ReactMethod
    public void getPackageName(Callback callback) {
        String packageName = getReactApplicationContext().getPackageName();
        callback.invoke(packageName);
    }







    //跳转手机设置
    @ReactMethod
    public void openNetworkSettings(Callback cb) {
        Activity currentActivity = getCurrentActivity();
        if (currentActivity == null) {
            cb.invoke(false);
            return;
        }
        try {
            currentActivity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
            cb.invoke(true);
        } catch (Exception e) {
            cb.invoke(e.getMessage());
        }
    }


}
